package com.ruoyi.web.controller.api.recipe;

import com.ruoyi.api.domain.recipe.CategoryEntity;
import com.ruoyi.api.domain.recipe.TagEntity;
import com.ruoyi.common.annotation.NoLoginRequired;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.recipe.domain.RecipeCategory;
import com.ruoyi.recipe.domain.RecipeTag;
import com.ruoyi.recipe.service.IRecipeTagService;
import com.ruoyi.web.controller.api.APIController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("食谱标签")
@RestController
@RequestMapping("/api/tag")
public class TagController extends APIController
{
    @Autowired
    private IRecipeTagService recipeTagService;

    @ApiOperation("获取全部食谱标签")
    @PostMapping("/list")
    @NoLoginRequired
    public AjaxResult list()
    {
        List<TagEntity> list = new ArrayList<TagEntity>();
        List<RecipeTag> tagList = recipeTagService.selectTagAll();
        for (RecipeTag tag : tagList)
        {
            TagEntity entity = new TagEntity();
            entity.setTagId(tag.getTagId());
            entity.setTagName(tag.getTagName());

            List<CategoryEntity> detailContent = new ArrayList<CategoryEntity>();
            for (RecipeCategory category : tag.getCategoryList())
            {
                CategoryEntity categoryEntity = new CategoryEntity();
                BeanUtils.copyBeanProp(categoryEntity, category);

                detailContent.add(categoryEntity);
            }
            entity.setDetailContent(detailContent);

            list.add(entity);
        }
        return AjaxResult.success(list);
    }
}
